<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="../include/taglib.jsp"%>
<%@page import="java.util.*"%>
<%@ page import="com.sinodata.bsm.common.constants.ResConstants" %>
<%@ page import="com.sinodata.bsm.center.bean.*" %>
<%@ page import="com.sinodata.bsm.common.vo.*" %>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<%
String ntype = (String)request.getAttribute("ntype");
String name = null;
Long id = null;
Long monitorTypeId = null;
if(ntype.equals("4")){
    MonitorType  monitorType   = (MonitorType)request.getAttribute("monitorType");
    name = monitorType.getName();
    id = monitorType.getId();
    monitorTypeId = id;
}else{
    ResGroup  resGroup   = (ResGroup)request.getAttribute("resGroup");
    name = resGroup.getName();
    id = resGroup.getId();
    monitorTypeId = resGroup.getMonitorTypeId();
}
List<Object[]> childInfo =(List<Object[]>)request.getAttribute("children");
List resList =(List)request.getAttribute("resList");

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>查看资源组</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="pragma" content="no-cache" />
		<meta http-equiv="cache-control" content="no-cache" />
		<meta http-equiv="expires" content="0" />
		<%@ include file="../include/common.jsp"%>

		<%@ include file="../include/table.jsp"%>
		<%@ include file="../include/jbox.jsp"%>
		<link rel="stylesheet" media="all" type="text/css" href="${pageContext.request.contextPath}/style/default/tree/zTreeStyle.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/raty/js/jquery.raty.min.js"></script>
		 <script type="text/javascript" src="${pageContext.request.contextPath}/js/tree/jquery.ztree.all-3.1.min.js"></script>
		 <link href="${pageContext.request.contextPath}/style/default/bsm/portal.css" charset="UTF-8" type="text/css" rel="stylesheet"/>
		<script type="text/javascript">
			function cancelAllCheck(){
				$("#dt_check_all").removeAttr("checked");
			}
	
	    	function allClick(isSigle)
	    	{
  	    		var dt_check_all = $('#dt_check_all').attr("checked");
	  	    	var allCheckbox = $("input[name='dt_check']");
	  	    	if(dt_check_all)
	  	    	{
	  	    		allCheckbox.attr("checked",allCheckbox);  //全选
	  	    		$('#resInfo tbody tr').addClass('row_selected');  //背景色全选
	  	    	}else
	  	    	{
	  	    		allCheckbox.attr("checked",false);   //取消全选
	  	    		$('#resInfo tbody tr').removeClass('row_selected');  //背景色取消
	  	    	}
  	    	
	    }
	    function addGroup() {
			var url ="iframe:resource/res-group!input.action?ntype=<%=ntype%>&pId=<%=id%>&viewId=${viewId}";
			var opt = { title: "新增资源组",width:530,height:230,buttons: {}};
			getTopWin(window).openjBox(url,opt,'res-group-input',window);
		}
		function updateGroup() {
			var url ="iframe:resource/res-group!initUpdate.action?id=<%=id%>";
			var opt = { title: "修改资源组",width:530,height:230,buttons: {}};
			getTopWin(window).openjBox(url,opt,'res-group-update',window);
		}
		function addGroupTree() {
			getSelectedNode = function(v, h, f){
				var tree = window.parent.parent.frames['jbox-iframe'].getGroupTree();
				var nodes = tree.getCheckedNodes(true);
				var mydata = {};
				mydata.Ids = new Array();
				for(var i=0; i<nodes.length; i++){
					var arr = nodes[i].id.split("|"); 
					if(arr[0]!=2){
						getTopWin(window).getMessageBox().alert(nodes[i].name +" 不是资源",'错误');
						v = false;
						break;
					}
					mydata.Ids.push(arr[1]);	
				}
				mydata.id = '<%=id%>';
				mydata.ntype = '<%=ntype%>';
				
				if(v){
					$.ajax({	
					type : "POST",
		    		url : "res-group!saveMotiResRel.action", //请求的action
		    		data : jQuery.param(mydata,true), //传的参数
		    		dataType : 'text',
		    		async:false,
					cache:false,
		    		success : function(data) { //结果
		    			if(data=='true'){
		    				window.parent.location.reload();
		    			}else{
		    				getTopWin(window).getMessageBox().alert(data,'不符合的资源信息');
		    				v = false;
		    			}
		    		}
		    	});
				}
				return v;
			}
			var url ="iframe:resource/res-group!tree.action?m=true";
			var opt = { title: "关联资源",width:230,height:480,submit: getSelectedNode};
			getTopWin(window).openjBox(url,opt,'res-group-tree',window);
		}
		function deleteGroupRel() {
			var oTable =$('#resInfo').dataTable();
				var rows  = $('#resInfo tr.row_selected');
				if(rows.length<1){
						getTopWin(window).getMessageBox().alert("请先在列表中选择资源。",'提示');
						return;
				}
				var mydata = {};
				mydata.Ids = new Array();
				for(var i = 0 ; i<rows.length;i++){
						var aData = oTable.fnGetData(rows[i]);
						mydata.Ids[i] = aData.id;
					}
	
				mydata.id = '<%=id%>';
				$.ajax({	
					type : "POST",
		    		url : "res-group!moveMotiResRel.action", //请求的action
		    		data : jQuery.param(mydata,true), //传的参数
		    		dataType : 'text',
		    		async:false,
					cache:false,
		    		success : function(data) { //结果
		    		/**	if(data=='true'){
		    				for(var i= rows.length-1; i>=0 ; i--)
		    					oTable.fnDeleteRow(rows[i]); 
		    			}*/
		    			window.parent.location.reload();
		    		}
		    	});
		}
		  function deleteGroup(id) {
				$.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/resource/res-group!delete.action?id="+id, //请求的action
						dataType : 'json',
						success : function(data) { //结果
							if(data == true){
									getTopWin(window).getMessageBox().tip('删除成功。', 'success',{timeout: 1000});
									window.parent.location.reload();
			    			}else{
			    				getTopWin(window).getMessageBox().alert("删除失败。","提示");
			    			}
							
						}
					});
					return false;
		}
	    $(document).ready(function(){
	    	$("#resInfo").dataTable({
				"bProcessing": true, 
				"aaData": ${resList},
				"bSort": true,
				"bPaginate": false,
				"iDisplayLength":10,
				"aLengthMenu": [10,20,30,50,100],
				"sDom":'rt <"bottom"flp>',
				"bFilter":false,
				"sPaginationType": 'full_numbers',
				"aoColumns":[
					{"mDataProp":"checkbox","bSortable": false},
					{"mDataProp":"id"},
					{"mDataProp":"ip","bSortable": false},
					{"mDataProp":"name","bSortable": false},
					{"mDataProp":"resTypeName","bSortable": false},
					{"mDataProp":"healthStatus","bSortable": false}
				],
				"oLanguage":{
					"sLengthMenu": "每页 _MENU_ 条",
	 		 		"sZeroRecords": "  ",
					"sInfo": "当前从 _START_ 到 _END_ 条,共 _TOTAL_ 条记录", 
					"sInfoEmpty": "没有找到记录",
					"oPaginate":{"sFirst":"首页","sPrevious":"上一页","sNext":"下一页","sLast":"尾页"}
				}
			});
		 $('#resInfo tbody td :checkbox').live('click', function (e) {
			var nTr = $(this).parents('tr')[0]; 
		    
			   if ($(nTr).hasClass('row_selected')) {
				  	$(nTr).removeClass('row_selected'); 
				  	cancelAllCheck();
			  }else
			  {
			  		$(nTr).addClass('row_selected'); 
			 }
			 if(!$(this).attr("checked")){
			 	$('#allCheck').attr("checked",false);
			 }
		});
			  var valueSpan = $("span:.health");
						$.each(valueSpan,function(index,ob){
							var id = $(ob).attr('id');
							var value = $(ob).attr('value');
							$('#'+id).raty({
								start: value/20,
								readOnly:  true,
								hintList:[ ''+value,''+value,''+value,''+value,''+value],
								path: '${pageContext.request.contextPath}/js/raty/img/'
							});
						});
			var ntype = <%=ntype%>;
			if(ntype==4){
				$("#deleteGroupRel").hide();
				$("#modifybt").hide();
			}
	    });
		</script>
	</head>
<body>
 <h2 class="title-h2"><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>资源组【<%=name %>】</h2>

 <div style=" background:#fafafa; overflow:hidden; 	border: 1px solid #afcbdf;">
 	<% if(!UserFunctionValidate.isGuestRole()){%>
	<div class ="toolbar" id="toolbar" >
			<a id = "modifybt" href="#" onclick="updateGroup()" class="page_edit">修改</a>
  			<span class="page_s" >&nbsp;</span>
  			<% 
  				if(childInfo.size()==0 && resList.size()==0 && ntype.equals("5")){
  			%>
  				<a id = "deleteGroupRel" href="#" onclick="deleteGroup(<%=id%>);" class="page_delete" >删除</a>
  				<span class="page_s" >&nbsp;</span>
  			<%
  				}
  			%>
  			<a href="#" onclick="addGroup()" class="page_add" >添加子资源组</a>
  		</div>
  		<% } %>
<%
	
	for(Object[]  objs: childInfo){
	    ResGroup g = (ResGroup)objs[0];
	    Integer[] info = (Integer[])objs[1];
%>
	<div class="por-div" >
	<ul class="xUl">
		<li style="margin-top:-8px; margin-bottom:5px;"><a href="#" class="xUla"><img src="${pageContext.request.contextPath}/style/default/images/equi/mac.gif" /></a></li>
		<li>
			<span><%=g.getName() %></span>
			<span>
			<%

				if(info[1]!=0){
				    out.println("( "+info[1]+" )");
				}else{
				    out.println(" <a style='cursor:pointer;	color:blue;' onclick='deleteGroup("+g.getId()+")'>删除</a>");
				}
			%>
				
			</span>
			
		</li>
	</ul>
</div>

<%} %>


</div>
 <h2 class="title-h2"><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>关联资源列表</h2>
  			<% if(!UserFunctionValidate.isGuestRole()){%>
  		<div class ="toolbar" id="toolbar" >
  			<a href="#" onclick="addGroupTree();" class="page_add" >关联资源</a>
  			<span class="page_s" >&nbsp;</span>
  			<a id = "deleteGroupRel" href="#" onclick="deleteGroupRel();" class="page_delete" >删除关联</a>
  			<!--  <span class="page_s" >&nbsp;</span>
  			<a href="#" onclick="applyWindow()" class="page_edit" >删除资源</a>
  			-->
  		</div>
  		<% } %>
		<table width="100%"  id="resInfo" cellpadding="0" cellspacing="0" border="0" class="display"  >
			<thead>
    	 		<tr>
    	 			<th width="1%" >&nbsp;&nbsp;<input type='checkbox' name='dt_check_all'
					id="dt_check_all" onclick="allClick()" /></th>
    	 			<th width="8%">资源ID</th>
    	 			<th width="15%">资源IP</th>
    	 			<th width="25%">资源名称</th>
    	 			<th width="20%">资源类别</th>
    	 			
    	 			<th>健康状态</th>
    	 		</tr>
    	 	</thead>
    	 </table>
 	
 
 
</body>
</html>
